{% extends 'base.html' %}

{% block title %}Create User{% endblock %}

{% block body %}
    <form class="layui-form layui-form-pane" action="">
        {% csrf_token %}
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" name="username" required lay-verify="required" placeholder="Enter username"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-inline">
                <input type="email" name="email" required lay-verify="email" placeholder="Enter email"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="password" name="password" required lay-verify="required" placeholder="Enter password"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input type="checkbox" checked="" name="is_enabled" lay-skin="switch" lay-filter="switchTest"
                       lay-text="是|否">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">角色选择</label>
            <div class="layui-input-inline">
                <select name="role" lay-verify="required" lay-search="">
                    <option value="">直接选择或搜索选择</option>
                    {% for role in role %}
                        <option value={{ role.id }}>{{ role.name }}</option>
                    {% endfor %}
                </select>
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 1%;">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="createUser">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
{% endblock %}
{% block js %}
    <script>
        layui.use(['jquery', 'form', 'layer'], function () {
            var form = layui.form
                , $ = layui.jquery
                , layer = layui.layer;

            form.on('submit(createUser)', function (data) {
                data = data.field;
                data['csrfmiddlewaretoken'] = $('[name="csrfmiddlewaretoken"]').val();
                $.ajax({
                    url: "{% url 'auth:user_api' %}",
                    type: 'POST',
                    data: data,
                    dataType: 'json',
                    success: function (res) {
                        if (res.code === 0){
                            layer.msg(res.msg, {icon: 6});
                            document.location.href = "{% url 'auth:user_list' %}", true;
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    },
                    error: function (error) {
                        layer.msg(error.msg, {icon: 5});
                    }
                })
                return false;
            });

        });
    </script>
{% endblock %}